/**
 * Created by tanbingqin on 2020/2/11.
 */
import {messages} from "share/common";
import React from 'react';
import { connect } from 'react-redux';
import errorMessage from 'share/errorMessage';
import { message, Button, Popconfirm, Icon, Form, Input, Switch, Row, Col, Radio, TimePicker } from 'antd';
const FormItem = Form.Item;
import consumerService from '../consumer.service'
import Chooser from 'components/chooser'

class CardEditFrame extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: false
    }
  }

  componentDidMount() {};

  onCancel = () => {
    this.props.close();
  };

  handleSave = () => {
    const { form, close, record, type } = this.props;
    form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        let supplierCode = type === 'myacard' ? '001' : '002';
        let params = {
          companyOID: record.companyOID,
          status: values.enable ? 1001 : 1002,
          mappingId: values.enable ? (type === 'myacard' ? values.mappingId : values.mappingId[0].id) : ''
        };
        this.setState({loading: true});
        consumerService.updateCardConfig(supplierCode, params).then(res => {
          this.setState({loading: false});
          close(true);
        }).catch(err => {
          this.setState({loading: false});
          errorMessage(err.response);
        });
      }
    });
  };

  render() {
    const { getFieldDecorator, getFieldValue } = this.props.form;
    const { loading } = this.state;
    const { record, type } = this.props;
    const formItemLayout = {
      labelCol: {span: 6},
      wrapperCol: {span: 16, offset: 1}
    };
    return (
      <div className="new-form">
        <Form>
          <FormItem {...formItemLayout} label={messages('supplier-1.key130')/*公司代码*/}>
            <Input
              disabled
              value={record.companyCode}
            />
          </FormItem>
          <FormItem {...formItemLayout} label={messages('supplier-1.key33')/*公司名称*/}>
            <Input
              disabled
              value={record.companyName}
            />
          </FormItem>
          <FormItem {...formItemLayout} label={messages('supplier-1.key179')/*开启状态*/}>
            {getFieldDecorator('enable', {
              valuePropName: 'checked',
              initialValue: record ? record.status === 1001 : true
            })(
              <Switch />
            )}
          </FormItem>
          {getFieldValue('enable') && type === 'myacard' && (
            <FormItem {...formItemLayout} label={messages('supplier-1.key131')/*关联公司ID*/}>
              {getFieldDecorator('mappingId', {
                rules: [
                  {
                    required: true,
                    message: messages('supplier-1.key103')/*请输入temp2*/
                  }
                ],
                initialValue: record ? record.mappingId : ''
              })(
                <Input placeholder={messages('supplier-1.key103')/*请输入temp2*/} />
              )}
              <div style={{color: '#989898'}}>{messages('supplier-1.key180')/*此处填写在领创开通服务的对应的公司ID，可通过领创服务人员获取。*/}</div>
            </FormItem>
          )}
          {getFieldValue('enable') && type === 'rebeccard' && (
            <FormItem {...formItemLayout} label={messages('supplier-1.key181')/*公司扣款账户*/}>
              {getFieldDecorator('mappingId', {
                rules: [
                  {
                    required: true,
                    message: messages('common.select')/*请选择*/
                  }
                ],
                initialValue: (record && record.mappingId) ? [{bankAccountShowName: `${record.accountName}${record.accountNo}`, id: record.mappingId}] : undefined
              })(
                <Chooser
                  placeholder={messages('common.select')/*请选择*/}
                  type='bank_card_account'
                  labelKey='bankAccountShowName'
                  valueKey='id'
                  listExtraParams={{
                    companyId: record.companyId,
                    bankName: '民生银行'
                  }}
                  showNumber={false}
                  single={true}
                />
              )}
              <div style={{color: '#989898'}}>{messages('supplier-1.key182')/*此处选择可用于扣款的民生银行企业账户，须先维护公司银行账户。*/}</div>
            </FormItem>
          )}
        </Form>
        <div className="slide-footer">
          <Button type="primary" onClick={this.handleSave} loading={loading}>{messages('common.ok')/*确定*/}</Button>
          <Button onClick={this.onCancel}>{messages('common.cancel')/*取消*/}</Button>
        </div>
      </div>
    )
  }
}

function mapStateToProps(state) {
  return {}
}

CardEditFrame.propTypes = {};

CardEditFrame.defaultProps = {};

const WrappedCardEditFrame = Form.create()(CardEditFrame);

export default connect(mapStateToProps)(WrappedCardEditFrame);
